package com.study.mybatisplus.controller;

import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.mybatisplus.entity.User;
import com.study.mybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author yuanZhi
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	/**
	 * 查询所有
	 */
	@GetMapping("get/all")
	public List<User> getAll() {
		return userService.list();
	}

	/**
	 * 分页查询
	 * @param current 当前页
	 * @param size 页容量
	 */
	@GetMapping("/get/page")
	public IPage<User> getPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
			@RequestParam(value = "size", defaultValue = "7") Integer size) {
		Page<User> page = new Page<>(current, size);
		Page<User> userPage = userService.page(page);
		return userService.getPage(userPage);
	}

	/**
	 * 根据主键id查询
	 *
	 * @param id
	 *            主键id
	 */
	@GetMapping("get/{id}")
	public User findById(@PathVariable("id") Long id) {
		return userService.getById(id);
	}

	/**
	 * 根据条件查询 SELECT
	 * id,account,password,name,type,state,created_by,created_time,updated_by,updated_time
	 * FROM t_user WHERE type = ? AND account = ?
	 */
	@GetMapping("get/map")
	public List<User> findByMap() {
		Map<String, Object> map = new HashMap<>();
		map.put("account", "zyz");
		map.put("type", 2);
		return userService.listByMap(map);
	}

	/**
	 * 根据条件构造器查询 当下面的queryWrapper为空时，表示查询所有
	 *
	 * @return
	 */
	@GetMapping("get/wrapper")
	public List<User> findByWrapper(
			@RequestParam(value = "account", required = false) String account,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "state", required = false) Integer state) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		if (Objects.nonNull(account)) {
			queryWrapper.eq("account", account);
		}
		if (Objects.nonNull(name)) {
			queryWrapper.eq("name", name);
		}
		if (Objects.nonNull(state)) {
			queryWrapper.eq("state", state);
		}
		return userService.list(queryWrapper);
	}

	/**
	 * 根据条件构造器查询 条件不等时
	 */
	@GetMapping("get/wrapper/non")
	public List<User> findByWrapperNonNul(
			@RequestParam(value = "name", required = false) String name) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("name", name);
		return userService.list(queryWrapper);
	}

	/**
	 * 根据条件构造器模糊查询并降序排列
	 */
	@GetMapping("get/wrapper/like")
	public List<User> findByWrapperLike(
			@RequestParam(value = "account", required = false) String account) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		// 左匹配就是leftLike，右匹配rightLike
		queryWrapper.like("account", account).orderByDesc("id");
		return userService.list(queryWrapper);
	}

	/**
	 * 根据条件构造器 in查询
	 */
	@GetMapping("get/wrapper/in")
	public List<User> findByWrapperIn(
			@RequestParam(value = "account", required = false) String account) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		// 不在范围内notInSql
		queryWrapper.inSql("account", account);
		return userService.list(queryWrapper);
	}

	/**
	 * 根据条件构造器 or查询
	 */
	@GetMapping("get/wrapper/or")
	public List<User> findByWrapperOr(
			@RequestParam(value = "account", required = false) String account,
			@RequestParam(value = "name", required = false) String name) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("account", account).or().eq("name", name)
				.orderByDesc("id");
		return userService.list(queryWrapper);
	}

	/**
	 * 根据条件构造器 apply查询(自己拼写sql)
	 */
	@GetMapping("get/wrapper/apply")
	public List<User> findByWrapperApply(
			@RequestParam(value = "account", required = false) String account) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		// todo
		queryWrapper.apply("account = " + account);
		return userService.list(queryWrapper);
	}

	/**
	 * 根据主键删除
	 */
	@DeleteMapping("delete/{id}")
	public void deleteById(@PathVariable("id") Long id) {
		userService.removeById(id);
	}

	/**
	 * 添加
	 */
	@PostMapping("save")
	public void save(@RequestBody User user) {
		userService.save(user);
	}

	/**
	 * 使用Wrapper自定义sql查询
	 */
	@GetMapping("get/self/type")
	public List<User> getSelfDefine() {
		return userService.getAllByType();
	}

}
